using Ease.EaseNode.Editor.NodeView;
using Ease.EaseNode.Runtime;
using Ease.EaseNode.Runtime.Param;
using Ease.TestEaseNode.Runtime;
using Ease.TestEaseNode.Runtime.Param;
using UnityEngine;
using UnityEngine.UIElements;

namespace Ease.TestEaseNode.Editor
{
    public sealed class TestEaseNode2 : BaseEaseNode
    {
        private TestData2 testData2 = null;
        private TextField textFieldName = null;
        private TextField textFieldContent = null;

        public TestEaseNode2(IEaseNodeDataState state) : base(state)
        {
            title = state == null ? "TestEaseNode" : $"TestEaseNode_{state.Key}";
            contentContainer.style.backgroundColor = Color.gray;
            textFieldName = new TextField();
            textFieldName.RegisterCallback<ChangeEvent<string>>(OnTextFieldNameChange);
            textFieldContent = new TextField();
            textFieldContent.multiline = true;
            textFieldContent.RegisterCallback<ChangeEvent<string>>(OnTextFieldCotentChange);
            contentContainer.Add(textFieldName);
            contentContainer.Add(textFieldContent);
            testData2 = State.Data as TestData2;
            if (testData2 != null)
            {
                title = $"{testData2.DataDes}_{testData2.id}";
                textFieldName.SetValueWithoutNotify(testData2.name);
                textFieldContent.SetValueWithoutNotify(testData2.content);
            }
        }

        public override void OnNodeRemove()
        {
            base.OnNodeRemove();
            testData2 = null;
            textFieldName = null;
            textFieldContent = null;
        }

        protected override void OnStateChange(BaseChangeParam param)
        {
            if (param.Key == typeof(StateChangeParam).Name)
            {
                var param2 = param as StateChangeParam;
                switch (param2.newState)
                {
                    case NodeState.None:
                        contentContainer.style.backgroundColor = Color.gray;
                        break;
                    case NodeState.Start:
                        contentContainer.style.backgroundColor = Color.green;
                        break;
                    case NodeState.Running:
                        contentContainer.style.backgroundColor = Color.green;
                        break;
                    case NodeState.Finish:
                        contentContainer.style.backgroundColor = Color.red;
                        break;
                }
            }
            else if (param.Key == typeof(StringChangeParam).Name)
            {
                var param2 = param as StringChangeParam;
                if (param2.valueKey.Equals("name"))
                    textFieldName.SetValueWithoutNotify(param2.value);
                else if (param2.valueKey.Equals("content"))
                    textFieldContent.SetValueWithoutNotify(param2.value);
            }
            else if (param.Key == typeof(SettingChangeParam).Name)
            {
                var param2 = param as SettingChangeParam;
                contentContainer.style.backgroundColor = param2.NodeBGColor;
            }
        }

        private void OnTextFieldNameChange(ChangeEvent<string> a)
        {
            testData2.name = a.newValue;
        }

        private void OnTextFieldCotentChange(ChangeEvent<string> a)
        {
            testData2.content = a.newValue;
        }
    }
}